﻿Imports System.Data
Imports MySql.Data.MySqlClient
Public Class MProducts
    Dim connector As New MySqlConnection("server=127.0.0.1;database='commerce';user='root';password='';")
    Dim command As MySqlCommand
    Dim datareader As MySqlDataReader
    Private Sub MProducts_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Me.WindowState = FormWindowState.Maximized
        connector.Open()
        command = New MySqlCommand("select * from products", connector)
        datareader = command.ExecuteReader()
        While datareader.Read
            comID.Items.Add(datareader.Item("id"))
        End While
        datareader.Close()
        command = New MySqlCommand("select * from manufacturers", connector)
        datareader = command.ExecuteReader()
        While datareader.Read
            comManufacturer.Items.Add(datareader.Item("id"))
        End While
        datareader.Close()
        connector.Close()
    End Sub

    Private Sub comID_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles comID.SelectedIndexChanged
        txtName.Enabled = True
        comManufacturer.Enabled = True
        connector.Open()
        command = New MySqlCommand("select * from products where id=" & comID.SelectedItem, connector)
        datareader = command.ExecuteReader()
        While datareader.Read
            txtName.Text = datareader.Item("name")
            comManufacturer.Text = datareader.Item("manufacturer")
        End While
        datareader.Close()
        connector.Close()
    End Sub

    Private Sub txtName_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles txtName.TextChanged
        If Me.txtName.Text = "" Then
            Me.cmdSave.Enabled = False
        Else
            Me.cmdSave.Enabled = True
        End If
    End Sub

    Private Sub cmdSave_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdSave.Click
        connector.Open()
        command = New MySqlCommand("update products set name='" & txtName.Text & "', manufacturer=" & comManufacturer.Text & " where id=" & comID.SelectedItem, connector)
        command.ExecuteNonQuery()
        MsgBox(txtName.Text & " modified")
        connector.Close()
    End Sub
End Class